<?php

namespace app\api\model;

use app\common\model\User;
use think\Model;

/**
 * 模型
 */
class Deliverystaff extends Model
{
    // 表名
    protected $name = 'delivery_staff';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    //拍照上传
    public function uploadimg($order_sn,$remark,$images,$deliverystaff_id)
    {
        $order = Order::where('order_sn', $order_sn)->where('deliverystaff_id', $deliverystaff_id)->find();
        if (!$order)throw new \Exception('未找到订单记录');
        $order->deliverystaff_remark = $remark;
        $order->deliverystaff_images = $images;
        $order->deliverystaff_upload_time = time();
        $res = $order->save();
        OrderAction::push($order->order_sn, '系统', '订单拍照上传');
        return $res;
    }

    //确认送达
    public function takedelivery($order_sn,$deliverystaff_id,$deliverystaff_realname)
    {
        $order = Order::where('order_sn', $order_sn)->where('deliverystaff_id', $deliverystaff_id)->find();
        if (!$order)throw new \Exception('未找到订单记录');
        if ($order->shippingstate == 2)throw new \Exception('已送达,请勿重复点击');
        $order->shippingstate = 2;
        $order->orderstate = 3;
        $order->receivetime = time();
        $res = $order->save();
        OrderAction::push($order->order_sn, '系统', '订单确认送达成功');

        $user = User::get($order->user_id);
        $auth = new \app\common\library\Auth();
        $param_data = array(
            'character_string8'=>array('value'=>$order_sn),
            'time7'=>array('value'=>date('Y-m-d H:i:s',time())),
            'thing4'=>array('value'=>'配送员:'.$deliverystaff_realname.'确认送达'),
        );
        $page = '/pageA/orderInfo/orderInfo?oid='.$order_sn;
        $served_template_id = \app\common\model\Config::get(['name'=>'served_template_id'])->value;
        $template_res = $auth->sendmessage($user->wechat_open_id,$page,$param_data,$served_template_id);

        return $res;
    }

    //开始配送
    public function delivery($order_sn,$deliverystaff_id)
    {
        $order = Order::where('order_sn', $order_sn)->where('deliverystaff_id', $deliverystaff_id)->find();
        if (!$order)throw new \Exception('未找到订单记录');
        if ($order->shippingstate == 1)throw new \Exception('已经开始配送,请勿重复点击');
        $order->shippingstate = 1;
        $order->start_shippingtime = time();
        $res = $order->save();
        OrderAction::push($order->order_sn, '系统', '订单开始配送');
        return $res;
    }

    //批量开始配送
    public function batchdelivery($order_ids,$deliverystaff_id)
    {
        $order_list = Order::where('id','in',$order_ids)
            ->where('deliverystaff_id', $deliverystaff_id)
            ->where('shippingstate', 0)
            ->field('order_sn')->select();

        foreach ($order_list as $k=>$v){
            $order = Order::where('order_sn', $v['order_sn'])->find();
            //if (!$order)throw new \Exception('未找到订单记录');
            //if ($order->shippingstate == 1)throw new \Exception('已经开始配送,请勿重复点击');
            $order->shippingstate = 1;
            $order->start_shippingtime = time();
            $res = $order->save();
            OrderAction::push($order->order_sn, '系统', '订单开始配送');
        }
        return true;
    }

    //订单列表
    public function orderlist($param,$deliverystaff_id)
    {
        $param['deliverystaff_id'] = $deliverystaff_id;

        $list = Order::with(['orderGoods'])
            ->where(function ($query) use ($param) {
                $query->where('paystate', 1);
                if (isset($param['deliverystaff_id']) && !empty($param['deliverystaff_id'])) {
                    $query->where('deliverystaff_id', $param['deliverystaff_id']);
                }

                if (isset($param['community_id']) && !empty($param['community_id'])) {
                    $query->where('community_id', $param['community_id']);
                }

                if (isset($param['date']) && !empty($param['date'])) {
                    $query->where('date', $param['date']);
                }

                if (isset($param['shippingtime']) && !empty($param['shippingtime'])) {
                    $query->where('shippingtime', $param['shippingtime']);
                }

                if (isset($param['status']) && $param['status'] != 'all') {
                    if ($param['status']=='wait_delivery'){
                        $query->where('orderstate', 0);
                        $query->where('shippingstate', 0);
                    } elseif ($param['status']=='delivering'){
                        //配送中
                        $query->where('orderstate', 0);
                        $query->where('shippingstate', 1);
                    } elseif ($param['status']=='complete'){
                        //已完成
                        $query->where('orderstate', 3);
                    } elseif ($param['status']=='refund'){
                        //售后
                        $query->where('orderstate', 4);
                    }

                }

                if (isset($param['q']) && $param['q'] != '') {
                    $query->where('order_sn', 'in', function ($query) use ($param) {
                        return $query->name('shop_order_goods')->where('order_sn|title', 'like', '%' . $param['q'] . '%')->field('order_sn');
                    });
                }
            })
            ->order('createtime desc')->paginate(10, false, ['query' => request()->get()]);

        return $list;
    }


}